<!--
 * @Author: szx
 * @Date: 2022-06-07 16:17:51
 * @LastEditTime: 2022-06-07 16:34:11
 * @Description: 
 * @FilePath: \learn\HTML\其他\滚轮视差响应效果\index.html
-->
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滚轮视差响应效果</title>
    <link rel="stylesheet" href="index.css">
</head>

<body>
    <h2>👇往下滚动一下</h2>
    <div class="bg">
        <span>Hello</span>
    </div>
    <h1>欢迎来到<br>我的世界</h1>
    <script type="text/javascript">
        const bg = document.querySelector('.bg')
        document.addEventListener('scroll', () => {
            const scrollY = window.scrollY;
            if (scrollY != 0)
                bg.style.backgroundPosition = `calc(50% + ${scrollY}px) calc(50% + ${scrollY}px) `
            else bg.style.backgroundPosition = ''
        })

    </script>
</body>

</html>